﻿using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    public class QueImageItem:UIBase<QueImageItem>
    {

        public Image QueImage;
        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
        }

        public void Init(string resPath)
        {
            if (ExamPanel._Instance != null)
            {
                ExamPanel._Instance.StartCoroutine(WWWLoadManager.Instance().LoadLocalResource(resPath, ShowImage));
            }
            else
            {
                CollectPanel._Instance.StartCoroutine(WWWLoadManager.Instance().LoadLocalResource(resPath, ShowImage));
            }
        }

        private void ShowImage(byte[]bytes)
        {
            Texture2D texture=new Texture2D((int)QueImage.rectTransform.rect.width,(int)QueImage.rectTransform.rect.height,TextureFormat.ARGB32, false);
            texture.LoadImage(bytes);
            QueImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),Vector2.zero);
            QueImage.GetComponentInParent<LayoutElement>().preferredHeight = QueImage.GetComponent<RectTransform>().rect.height + 50;
        }
    }
}